package com.wsoft.core.utils;

import com.wsoft.enums.PasswordLevelEnum;

import java.util.regex.Pattern;

/**
 * @Author Zhang gukai
 * @Date 2024/12/18 14:30
 */
public class PasswordValidator {

    private static final String LOWER_CASE_PATTERN = ".*[a-z].*";
    private static final String UPPER_CASE_PATTERN = ".*[A-Z].*";
    private static final String DIGIT_PATTERN = ".*\\d.*";
    private static final String SPECIAL_CHAR_PATTERN = ".*[~!@#$%^&*()\\-_=+\\\\|\\[{\\]};:'\",<.>/?].*";
    /**
     * 校验密码等级
     *
     * @param password 密码
     * @return 密码等级（LOW, MEDIUM, HIGH）
     */
    public static PasswordLevelEnum checkPasswordStrength(String password) {
        if (password == null || password.length() < 6) {
            return PasswordLevelEnum.LOW;
        }

        int strength = 0;

        // 检查是否包含小写字母
        if (Pattern.matches(LOWER_CASE_PATTERN, password)) {
            strength++;
        }

        // 检查是否包含大写字母
        if (Pattern.matches(UPPER_CASE_PATTERN, password)) {
            strength++;
        }

        // 检查是否包含数字
        if (Pattern.matches(DIGIT_PATTERN, password)) {
            strength++;
        }

        // 检查是否包含特殊字符
        if (Pattern.matches(SPECIAL_CHAR_PATTERN, password)) {
            strength++;
        }

        // 根据强度返回密码等级
        if (strength >= 3 && password.length() >= 10) {
            return PasswordLevelEnum.HIGH;
        } else if (strength >= 2 && password.length() >= 8) {
            return PasswordLevelEnum.MEDIUM;
        } else {
            return PasswordLevelEnum.LOW;
        }
    }

}
